//
//  FKMyProfitVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/29.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKMyProfitVC.h"
#import "FKWebViewVC.h"
#import "FKWithdrawVC.h"
#import "FKBankCardListVC.h"
#import "FKWithdrawDetailVC.h"
#import "FKIncomeDetailVC.h"
static NSString *const ID = @"UITableViewCell";

@interface FKMyProfitVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSArray *datas;

@property (nonatomic, weak) UILabel *amountLabel;
@property (nonatomic, weak) UILabel *totalProfitLabel;

@property (nonatomic, weak) UILabel *lastMonthPorfit;
@property (nonatomic, weak) UILabel *lastMonthEvaluatePorfit;
@property (nonatomic, weak) UILabel *thisMonthPorfit;
@property (nonatomic, weak) UILabel *thisMonthEvaluatePorfit;

@property (nonatomic, strong) NSDictionary *userInfo;
@property (nonatomic, strong) NSDictionary *profitIntro;

@end

@implementation FKMyProfitVC

- (NSArray *)datas
{
    if (!_datas) {
//        _datas = [NSArray array];
        _datas = @[@{@"icon" : @"earnings_icon_card", @"title" : @"银行卡"},
                   @{@"icon" : @"earnings_icon_detail", @"title" : @"提现明细"},
                   @{@"icon" : @"earnings_icon_order", @"title" : @"收益明细"}];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的收益";
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.tableFooterView = [UIView new];
    tableView.separatorInset = UIEdgeInsetsMake(0, 12, 0, 12);
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, fk_adjustW(410) + fkOffsetTop)];
    header.backgroundColor = [UIColor whiteColor];
    tableView.tableHeaderView = header;
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, fk_adjustW(206) + fkOffsetTop)];
    [header addSubview:topView];
    topView.backgroundColor = fkMainColor;
    
    UIImageView *topBgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"earnings_bg"]];
    [topView addSubview:topBgView];
    
    UILabel *amountLabel = [UILabel fk_labelWithFont:fk_adjustFont(25) textColor:[UIColor whiteColor] text:@"--"];
    [topView addSubview:amountLabel];
    self.amountLabel = amountLabel;
    
    UILabel *amountTitle = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:[UIColor whiteColor] text:@"可提现金额"];
    [topView addSubview:amountTitle];
    
    UIButton *goWithdrawBtn = [UIButton fk_btnWithTarget:self action:@selector(goWithdrawBtnClicked:) font:fk_adjustFont(12) normalTitle:@"去提现" normalTitleColor:fkMainColor selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage imageNamed:@"bg_white"] selectedBgImg:nil];
    [topView addSubview:goWithdrawBtn];
    [goWithdrawBtn fk_viewCornerRadius:fk_adjustW(10) borderWidth:0 borderColor:nil];
    
    UILabel *totalProfitLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:[UIColor whiteColor] text:@"累计结算收益:--"];
    [topView addSubview:totalProfitLabel];
    self.totalProfitLabel = totalProfitLabel;
    
    // 佣金
    CGFloat profitViewY = topView.fk_bottom - fk_adjustW(23);
    UIImageView *profitView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profit_bg"]];
    [header addSubview:profitView];
    profitView.contentMode = UIViewContentModeScaleToFill;
    profitView.frame = CGRectMake(0, profitViewY, fkScreenW, fk_adjustW(228));
//    profitView.backgroundColor = [UIColor greenColor];
    
    UIView *profitTopView = [[UIView alloc] initWithFrame:CGRectMake(12, 0, fkScreenW - 24, fk_adjustW(35))];
    [profitView addSubview:profitTopView];
//    profitTopView.backgroundColor = [UIColor fk_colorWithHex:0xFFBC3A];
    
    UIView *dotView = [[UIView alloc] init];
    [profitTopView addSubview:dotView];
    dotView.backgroundColor = [UIColor whiteColor];
    
    UILabel *profitTitle = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:[UIColor whiteColor] text:@"佣金收益"];
    [profitTopView addSubview:profitTitle];
    
    UIView *profitLine = [[UIView alloc] init];
    [profitView addSubview:profitLine];
    profitLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    NSArray *titles = @[@"\n结算收入", @"\n预估收入", @"\n结算收入", @"\n预估收入"];
    NSArray *titles1 = @[@"\n上月结算", @"\n上月佣金", @"\n本月结算", @"\n本月佣金"];
    CGFloat labelW = fk_adjustW(120);
    CGFloat labelH = fk_adjustW(90);
    CGFloat centerMargin = profitView.fk_width - 24 - 14 - labelW * 2;
    CGFloat beginLabelX = 12 + 7;
    CGFloat beginLabelY = profitTopView.fk_bottom;
    for (NSInteger index = 0; index < titles.count; index++) {
//        NSString *title = index % 2 == 0 ? @"--\n结算收入\n本月结算" : @"--\n预估收入\n本月佣金";
        NSString *text2 = titles[index];
        NSString *text3 = titles1[index];;
        NSMutableAttributedString *attr1 = [@"--" fk_addAttr:fkMainColor font:fk_adjustBoldFont(16)];
        NSMutableAttributedString *attr2 = [text2 fk_addAttr:fkColor333333 font:fk_adjustFont(16)];
        NSMutableAttributedString *attr3 = [text3 fk_addAttr:fkColor666666 font:fk_adjustFont(12)];
        [attr1 appendAttributedString:attr2];
        [attr1 appendAttributedString:attr3];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:6];
        [paragraphStyle setAlignment:NSTextAlignmentCenter];
        [attr1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attr1.length)];
        UILabel *label = [UILabel fk_labelWithFont:fk_adjustFont(16) textColor:fkMainColor];
        [profitView addSubview:label];
        label.attributedText = attr1;
        label.numberOfLines = 3;
        label.tag = 10 + index;
        
        CGFloat labelX = index % 2 == 0 ? beginLabelX : beginLabelX + labelW + centerMargin;
        NSInteger row = index / 2;
        CGFloat labelY = beginLabelY + row * labelH;
        label.frame = CGRectMake(labelX, labelY, labelW, labelH);
    }
    self.lastMonthPorfit = [profitView viewWithTag:10];
    self.lastMonthEvaluatePorfit = [profitView viewWithTag:11];
    self.thisMonthPorfit = [profitView viewWithTag:12];
    self.thisMonthEvaluatePorfit = [profitView viewWithTag:13];
    
    
    [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(fk_adjustW(112));
    }];
    [amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.offset(fkNavBarH + fkOffsetTop + fk_adjustW(17));
    }];
    [amountTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(amountLabel.mas_bottom).offset(fk_adjustW(10));
    }];
    [goWithdrawBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(fk_adjustW(60));
        make.height.offset(fk_adjustW(20));
        make.centerX.offset(0);
        make.top.equalTo(amountTitle.mas_bottom).offset(fk_adjustW(8));
    }];
    [totalProfitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(goWithdrawBtn.mas_bottom).offset(fk_adjustW(10));
        make.centerX.offset(0);
    }];
    [dotView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(6));
        make.left.offset(15);
        make.centerY.offset(0);
    }];
    [profitTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(dotView.mas_right).offset(4);
        make.centerY.offset(0);
    }];
    [profitLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(7);
        make.right.offset(-7);
        make.height.offset(0.8);
        make.bottom.offset(fk_adjustW(-92));
    }];
    
    
    [self setupNav];
    self.rightImgName = @"earnings_icon_introduce";
    
    [self loadData];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:@"refreshBalance" object:nil];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self loadData];
}

//- (void)dealloc
//{
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}

- (void)rightItemClicked
{
    if (self.profitIntro) {
        FKWebViewVC *vc = [[FKWebViewVC alloc] init];
        vc.htmlStr = [self.profitIntro[@"info"] fk_htmlToString];
        vc.title = @"收益介绍";
        fkPushNav(vc);
    }
}

- (void)goWithdrawBtnClicked:(UIButton *)btn
{
    if (self.userInfo) {
        NSString *balance = self.userInfo[@"withdrawPorfit"];
        if ([balance doubleValue] > 0) {
            FKWithdrawVC *vc = [[FKWithdrawVC alloc] init];
            vc.balance = balance;
            fkPushNav(vc);
        } else {
            [SVProgressHUD showInfoWithStatus:@"暂无可提现金额"];
        }
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        UIImageView *iconView = [[UIImageView alloc] init];
        [cell.contentView addSubview:iconView];
        iconView.tag = 10;
        
        UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(15) textColor:fkColor333333];
        [cell.contentView addSubview:titleLabel];
        titleLabel.tag = 11;
        
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(12);
            make.width.height.offset(fk_adjustW(18));
            make.centerY.offset(0);
        }];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(iconView.mas_right).offset(fk_adjustW(10));
            make.centerY.offset(0);
        }];
    }
    UIImageView *iconView = [cell.contentView viewWithTag:10];
    UILabel *titleLabel = [cell.contentView viewWithTag:11];
    NSDictionary *data = self.datas[indexPath.row];
    iconView.image = [UIImage imageNamed:data[@"icon"]];
    titleLabel.text = data[@"title"];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(45);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    if (indexPath.row == 0) {
        FKBankCardListVC *vc = [[FKBankCardListVC alloc] init];
        fkPushNav(vc);
    } else if (indexPath.row == 1) {
        FKWithdrawDetailVC *vc = [[FKWithdrawDetailVC alloc] init];
        fkPushNav(vc);
    } else if (indexPath.row == 2) {
        FKIncomeDetailVC *vc = [[FKIncomeDetailVC alloc] init];
        fkPushNav(vc);
    }
}

#pragma mark - 网络请求
- (void)loadData
{
    [FKHttpTool postWithSuffix:@"m=Home&c=Member&a=myPorfit" param:nil success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            NSDictionary *userInfo = result.data[@"userInfo"];
            self.userInfo = userInfo;
            self.profitIntro = result.data[@"porfitIntro"];
            NSDictionary *withdrawInfo = result.data[@"withdrawInfo"];
            
            self.amountLabel.text = fkFORMAT(@"￥%@", userInfo[@"withdrawPorfit"]);
            self.totalProfitLabel.text = fkFORMAT(@"累计结算收益: ￥%@", userInfo[@"total_porfit"]);
            
            [self setProfitText:self.lastMonthPorfit text:fkFORMAT(@"%@", userInfo[@"lastMonthCalPorfit"])];
            [self setProfitText:self.lastMonthEvaluatePorfit text:fkFORMAT(@"%@", userInfo[@"lastMonthEvaluatePorfit"])];
            [self setProfitText:self.thisMonthPorfit text:fkFORMAT(@"%@", userInfo[@"thisMonthCalPorfit"])];
            [self setProfitText:self.thisMonthEvaluatePorfit text:fkFORMAT(@"%@", userInfo[@"thisMonthEvaluatePorfit"])];
        }
    } failure:nil];
}

- (void)setProfitText:(UILabel *)label text:(NSString *)text
{
    NSArray *texts = [label.text componentsSeparatedByString:@"\n"];
    NSString *text1 = text;
    NSString *text2 = [@"\n" stringByAppendingString:texts[1]];
    NSString *text3 = [@"\n" stringByAppendingString:texts[2]];
    NSMutableAttributedString *attr1 = [text1 fk_addAttr:fkMainColor font:fk_adjustBoldFont(16)];
    NSMutableAttributedString *attr2 = [text2 fk_addAttr:fkColor333333 font:fk_adjustFont(16)];
    NSMutableAttributedString *attr3 = [text3 fk_addAttr:fkColor666666 font:fk_adjustFont(12)];
    [attr1 appendAttributedString:attr2];
    [attr1 appendAttributedString:attr3];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:6];
    [paragraphStyle setAlignment:NSTextAlignmentCenter];
    [attr1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attr1.length)];
    label.attributedText = attr1;
}


@end
